package com.hanthink.web.controller.label;

import java.util.List;

import com.hanthink.common.core.domain.model.LoginUser;
import com.hanthink.common.utils.ServletUtils;
import com.hanthink.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hanthink.common.annotation.Log;
import com.hanthink.common.core.controller.BaseController;
import com.hanthink.common.core.domain.AjaxResult;
import com.hanthink.common.enums.BusinessType;
import com.hanthink.system.domain.label.LabelVerification;
import com.hanthink.system.service.label.ILabelVerificationService;
import com.hanthink.common.utils.poi.ExcelUtil;
import com.hanthink.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * labelController
 * 
 * @author Shuyu Li
 * @date 2023-02-07
 */
@RestController
@RequestMapping("/label/LabelVerification")
public class LabelVerificationController extends BaseController
{
    @Autowired
    private ILabelVerificationService labelVerificationService;

    @Autowired
    private TokenService tokenService;
    /**
     * 查询-列表
     */
    @PreAuthorize("@ss.hasPermi('label:LabelVerification:list')")
    @GetMapping("/list")
    public TableDataInfo list(LabelVerification labelVerification)
    {
        startPage();
        List<LabelVerification> list = labelVerificationService.selectLabelVerificationList(labelVerification);
        return getDataTable(list);
    }

    /**
     * 导出标签列表
     */
    @PreAuthorize("@ss.hasPermi('label:LabelVerification:export')")
    @Log(title = "-", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LabelVerification labelVerification)
    {
        List<LabelVerification> list = labelVerificationService.selectLabelVerificationList(labelVerification);
        ExcelUtil<LabelVerification> util = new ExcelUtil<LabelVerification>(LabelVerification.class);
        return util.exportExcel(list, "LabelVerification");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('label:LabelVerification:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(labelVerificationService.selectLabelVerificationById(id));
    }

    /**
     * 新增标签
     */
    @PreAuthorize("@ss.hasPermi('label:LabelVerification:add')")
    @Log(title = "-", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LabelVerification labelVerification)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        return toAjax(labelVerificationService.insertLabelVerification(labelVerification,operName));
    }

    /**
     * 修改标签
     */
    @PreAuthorize("@ss.hasPermi('label:LabelVerification:edit')")
    @Log(title = "-", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LabelVerification labelVerification)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        return toAjax(labelVerificationService.updateLabelVerification(labelVerification,operName));
    }

    /**
     * 删除标签
     */
    @PreAuthorize("@ss.hasPermi('label:LabelVerification:remove')")
    @Log(title = "-", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        return toAjax(labelVerificationService.deleteLabelVerificationByIds(ids,loginUser.getUser().getUserName()));
    }

    /**
     * 下载模板
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<LabelVerification> util = new ExcelUtil(LabelVerification.class);
        return util.importTemplateExcel("标签数据");
    }
    /**
     * 导入标签数据
     */
    @Log(title = "标签管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('label:LabelVerification:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<LabelVerification> util = new ExcelUtil(LabelVerification.class);
        List<LabelVerification> labelList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = labelVerificationService.importPlan(labelList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 一键启用功能
     */
    @PreAuthorize("@ss.hasPermi('label:LabelVerification:change')")
    @Log(title = "一键启用", businessType = BusinessType.UPDATE)
    @PostMapping("/openStatusByIds")
    public AjaxResult changeStatusByIds(@RequestBody Long[] ids)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        return toAjax(labelVerificationService.changeStatusByIds(ids,loginUser.getUser().getUserName()));
    }



}
